package com.hnkywl.sts.dao.jhgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.jhgl.Xsxy;
import com.hnkywl.sts.entity.jhgl.Xy;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.*;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-1
 * Time: 下午9:37
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class XsxyDao extends SimpleHibernateTemplate<Xsxy, Long> {
    public Page<Xsxy> findXyList(String xyhm, Long ssgsId, Long gysId, Long khId, String spzt, int pageNo, int pageSize) {
        Page<Xsxy> page = new Page<Xsxy>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Projections.sqlProjection("this_.type as type", new String[]{"type"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("id").as("id"))
                        .add(Projections.sqlProjection("xyzt as xyzt", new String[]{"xyzt"}, new Type[]{Hibernate.STRING}))
                        .add(Projections.sqlProjection("xyhm as xyhm", new String[]{"xyhm"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("kssj").as("kssj"))
                        .add(Property.forName("jssj").as("jssj"))
                        .add(Property.forName("nxyl").as("nxyl"))
                        .add(Property.forName("yxyl").as("yxyl"))
                        .add(Property.forName("bz").as("bz"))
                        .add(Property.forName("createUser.xm").as("zdr"))
                        .add(Property.forName("processInstanceId").as("processInstanceId"))
                        .add(Property.forName("kh.id").as("kh_id"))
                        .add(Property.forName("kh.qc").as("kh_qc")))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        if (StringUtils.isNotEmpty(xyhm))
            criteria.add(Property.forName("xyhm").like(xyhm, MatchMode.START));
        if (StringUtils.isNotEmpty(spzt))
            criteria.add(Property.forName("xyzt").eq(spzt));
        if (gysId != null)
            criteria.add(Restrictions.eq("gys.id", gysId));
        if (ssgsId != null)
            criteria.add(Restrictions.eq("nbgs.id", ssgsId));
        if (khId != null)
            criteria.add(Restrictions.eq("kh.id", khId));
        RegionDao.createDataRegion(criteria);
        return this.findByCriteria(page, criteria);
    }


}
